package models
import (
	"time"
	"github.com/astaxie/beego/orm"
//	."LiquidMeasurement/lib"
	"github.com/astaxie/beego"
)

type Room struct {
	Id          	uint64		`orm:"unique;auto"`
	Createtime    	time.Time `orm:"type(datetime);auto_now_add" `
	Updatetime    	time.Time `orm:"type(datetime);auto_now_add" `
	Floor			uint8
	RoomNum			string						//门牌号
}



func init() {
	// 需要在init中注册定义的model
	orm.RegisterModel(new(Room))
}

//通过ID获取房间数据
//@param 	jobID  string      用户的电话号码
//@return   user Admin      返回用户的数组
//@return   err	 error      返回的错误
func GetRoomById(id uint64) (model Room,err error) {
	model = Room{Id: id}
	o := orm.NewOrm()
	err = o.Read(&model, "id")
	if err == orm.ErrNoRows {
		beego.Info("查询不到")
	} else if err == orm.ErrMissPK {
		beego.Info("找不到主键")
	}
	//fmt.Println(user.JobID,user.Passwd)
	beego.Info("user get department",model)
	return model,err
}


//获取所有的房间号
//@return   rooms []Room   返回admin的数组
//@return   count int64      数组的数量

func GetRoomList ()(rooms []Room,count int64) {
	o := orm.NewOrm()
	room := new(Room)
	qs := o.QueryTable(room)
	qs.All(&rooms)
	count, _ = qs.Count()
	return rooms, count
}


//通过userID删除用户数据
//@param 	roomID   int64      用户的用户ID
//@return   	 	int64      返回删除数据的状态
//@return   err	 	error      返回的错误
func DelRoomsByID(roomID uint64) (int64, error) {
	defer func() {
		if r := recover();r!=nil{

		}
	}()
	o := orm.NewOrm()
	status, err := o.Delete(&Room{Id: roomID})
	return status, err
}




//添加床位
//@param u  *Bed      要添加的用户数组结构体
//@return    int64      返回用户的id
//@return    error      返回的错误
func AddRoom(u *Room) (int64, error) {
	o := orm.NewOrm()
	//	u.UserPw = Strtomd5(u.UserPw)
	BedID, err := o.Insert(u)
	return BedID, err
}




//更新床位
//@param 	u   Bed      更新用户的数组结构
//@return   	Bed      返回用户的数据
//@return   err	 error      返回的错误
func UpdateRoom(u *Room) (Room, error) {
	room := Room{Id:u.Id}
	o := orm.NewOrm()
	err := o.Read(&room, "id")
	if err == orm.ErrNoRows {
		beego.Info("查询不到")
		return room,err
	} else if err == orm.ErrMissPK {
		beego.Info("找不到主键")
		return room,err
	}
	//	bed.RoomID = u.RoomID
	//	bed.Priority = u.Priority

		room.Id = u.Id

		room.Floor = u.Floor

		room.RoomNum = u.RoomNum


	u.Updatetime = time.Now()
	if num, err := o.Update(&room); err == nil {
		beego.Info(num)
	}else{
		beego.Info("Update",err)
	}
	beego.Info("bed",room)
	return room, err
}